package com.nbcio.iot.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.iot.domain.bo.IotProductBo;
import com.nbcio.iot.domain.vo.IotProductVo;
import com.nbcio.iot.service.IIotProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物联网产品控制器
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/product")
public class IotProductController extends BaseController {

    private final IIotProductService productService;

    /**
     * 查询产品列表
     */
    @GetMapping("/list")
    public TableDataInfo<IotProductVo> list(IotProductBo bo, PageQuery pageQuery) {
        return productService.queryPageList(bo, pageQuery);
    }

    /**
     * 分页查询产品列表（兼容前端page请求）
     */
    @GetMapping("/page")
    public TableDataInfo<IotProductVo> page(IotProductBo bo, PageQuery pageQuery) {
        return productService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取产品详细信息
     */
    @GetMapping("/{productId}")
    public R<IotProductVo> getInfo(@PathVariable("productId") Long productId) {
        return R.ok(productService.queryById(productId));
    }

    /**
     * 新增产品
     */
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotProductBo bo) {
        return toAjax(productService.insertByBo(bo));
    }

    /**
     * 修改产品
     */
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotProductBo bo) {
        return toAjax(productService.updateByBo(bo));
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@PathVariable Long[] productIds) {
        return toAjax(productService.deleteWithValidByIds(List.of(productIds)));
    }
} 